package com.dementors.view;

/**
 * ***********************
 * author:raochen
 * time:2015/12/7.
 * name:
 * step:
 * ***********************
 */

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dementors.R;
import com.dementors.base.SpaceItemDecoration;


/**
 *
 * @author jabony
 * @since 2015年3月31日 16:06:59
 *
 */
public class PullToRefreshRecyclerView extends LinearLayout{
    private ImageView iv_foot;
    /**
     * 底部上拉刷新的布局
     */
    private RelativeLayout rl_foot;
    /**
     * 上下文环境
     */
    private Context context;
    /**
     * 垂直方向
     */
    static final int VERTICAL = LinearLayoutManager.VERTICAL;
    /**
     * 水平方向
     */
    static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
    /**
     * 内容控件
     */
    private RecyclerView recyclerView;
    /**
     * 刷新布局控件
     */
    private SwipeRefreshLayout swipeRfl;
    private LinearLayoutManager layoutManager;
    /*
     * 刷新布局的监听
     */
    private OnRefreshListener mRefreshListener;
    /**
     * 内容控件滚动监听
     */
    private RecyclerView.OnScrollListener mScrollListener;
    /**
     * 内容适配器
     */
    private RecyclerView.Adapter mAdapter;
    /*
     * 刷新加载监听事件
     */
    private RefreshLoadMoreListener mRefreshLoadMoreListner;
    /**
     * 是否可以加载更多
     */
    private boolean hasMore = true;
    /**
     * 是否正在刷新
     */
    private boolean isRefresh = false;
    /**
     * 是否正在加载更多
     */
    private boolean isLoadMore = false;

    public PullToRefreshRecyclerView(Context context) {
        super(context);

    }

    @SuppressWarnings("deprecation")
    public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        // 导入布局
        LayoutInflater.from(context).inflate(R.layout.pulltorefreshrecyclerview,this,true);
        swipeRfl = (SwipeRefreshLayout) findViewById(R.id.all_swipe);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
      rl_foot= (RelativeLayout) findViewById(R.id.rl_foot);
        iv_foot= (ImageView) findViewById(R.id.iv_foot);

        iv_foot.setAnimation(AnimationUtils.loadAnimation(context,R.anim.anitmator_foot_refresh));
        // 加载颜色是循环播放的，只要没有完成刷新就会一直循环，color1>color2>color3>color4
        // swipeRfl.setColorScheme(Color.BLUE, Color.GREEN, Color.RED,
         //Color.YELLOW);
        swipeRfl.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        /**
         * 监听上拉至底部滚动监听
         */
        mScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //最后显示的项
                int lastVisibleItem = layoutManager
                        .findLastVisibleItemPosition();

                int totalItemCount = layoutManager.getItemCount();
                // lastVisibleItem >= totalItemCount - 4 表示剩下4个item自动加载
                // dy>0 表示向下滑动
				/*	if (hasMore && (lastVisibleItem >= totalItemCount - 1)
						&& dy > 0 && !isLoadMore) {
					isLoadMore = true;
					loadMore();
				}*/
                /**
                 * 无论水平还是垂直
                 */
                if (hasMore && (lastVisibleItem >= totalItemCount - 1)
                        && !isLoadMore) {
                    isLoadMore = true;
                   rl_foot.setVisibility(View.VISIBLE);
                    loadMore();
                }

                if( layoutManager.findFirstVisibleItemPosition()==0){
                    mRefreshLoadMoreListner.atTop();
                }


                if(layoutManager.findFirstVisibleItemPosition()>=5){
                    mRefreshLoadMoreListner.showButton();
                }
            }
        };

        /**
         * 下拉至顶部刷新监听
         */
        mRefreshListener = new OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isRefresh) {
                    isRefresh = true;
                    refresh();
                }
            }
        };
        recyclerView.addItemDecoration(new SpaceItemDecoration(20));
        swipeRfl.setOnRefreshListener(mRefreshListener);
        recyclerView.setHasFixedSize(false);
        layoutManager = new MyLinearLayoutManager(context, MyLinearLayoutManager.VERTICAL, false);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
         recyclerView.setLayoutManager(layoutManager);
        recyclerView.setOnScrollListener(mScrollListener);

    }

//    public void setOrientation(int orientation) {
//        if (orientation != 0 && orientation != 1) {
//            layoutManager.setOrientation(VERTICAL);
//        } else {
//            layoutManager.setOrientation(HORIZONTAL);
//        }
//    }
//
//    public int getOrientation() {
//        return layoutManager.getOrientation();
//    }

    public void setPullLoadMoreEnable(boolean enable) {
        this.hasMore = enable;
    }

    public boolean getPullLoadMoreEnable() {
        return hasMore;
    }

    public void setPullRefreshEnable(boolean enable) {
        swipeRfl.setEnabled(enable);
    }

    public boolean getPullRefreshEnable() {
        return swipeRfl.isEnabled();
    }

    public void setLoadMoreListener() {
        recyclerView.setOnScrollListener(mScrollListener);
    }

    public void loadMore() {
        if (mRefreshLoadMoreListner != null && hasMore) {
            mRefreshLoadMoreListner.onLoadMore();
        }
    }

    /**
     * 加载更多完毕,为防止频繁网络请求,isLoadMore为false才可再次请求更多数据
     */
    public void setLoadMoreCompleted(){
        isLoadMore = false;
      rl_foot.setVisibility(View.INVISIBLE);
    }

    public void stopRefresh() {
        isRefresh = false;
        swipeRfl.setRefreshing(false);
    }

    public void setRefreshLoadMoreListener(RefreshLoadMoreListener listener) {
        mRefreshLoadMoreListner = listener;
    }

    public void refresh() {
        if (mRefreshLoadMoreListner != null) {
            mRefreshLoadMoreListner.onRefresh();
        }
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        if (adapter != null)
            recyclerView.setAdapter(adapter);
    }

    public interface RefreshLoadMoreListener {
        public void onRefresh();

        public void onLoadMore();

        public void atTop();

        public  void  showButton();
    }

    public void addItemDecoration(RecyclerView.ItemDecoration Decoration){
        recyclerView.addItemDecoration(Decoration);
    }

    public void scrollToTop(){
        recyclerView.scrollToPosition(0);
    }

}